/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mti.javashare.dao;

import com.mti.javashare.beans.File;
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Sinheldrin
 */
public class FileDao implements IFileDao
{
    @PersistenceContext
    private EntityManager entityManager;

    public File getById(int id)
    {
        Query query = entityManager.createQuery("SELECT f FROM File f WHERE f.id = :id");
        query.setParameter("id", id);
        return (File) query.getSingleResult();
    }

    public File getByName(String name)
    {
        Query query = entityManager.createQuery("SELECT f FROM File f WHERE f.name like :name");
        query.setParameter("name", name);
        return (File) query.getSingleResult();
    }

    public Collection<File> getAll()
    {
        Query query = entityManager.createQuery("SELECT f FROM File f");
        return (Collection<File>) query.getResultList();
    }
    
    public void add(File file)
    {
        entityManager.persist(file);
    }

    public void update(File file)
    {
        entityManager.merge(file);
    }

    public void remove(File file)
    {
        entityManager.remove(file);
    }
}
